package com.dlc.beijingrfid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.dlc.commonlibrary.dialog.WaitingDialog;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.adapter.OddAdapter;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.net.bean.DistributeOrderBean;
import com.dlc.beijingrfid.net.bean.DistributeOrderDetailBean;
import com.dlc.beijingrfid.net.exception.ErrorMsgException;
import com.dlc.beijingrfid.net.protocol.StockProto;
import com.dlc.beijingrfid.net.protocol.base.HttpProtocol;
import com.holenzhou.pullrecyclerview.PullRecyclerView;
import com.holenzhou.pullrecyclerview.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/7/19 0019.
 */

public class DistributeOddActivity extends BaseActivity {
    @BindView(R.id.odd_rv)
    PullRecyclerView odd_rv;
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    private OddAdapter oddAdapter;
    private List<DistributeOrderBean.DataBean.ListBean> orderBeanList=new ArrayList<>();
    private int presentPage = 1;
    private boolean isRefresh = true;
    private int pageSize = 3;
    private WaitingDialog mDialog;
    @Override
    protected int getLayoutID() {
        return R.layout.activity_distribute_odd;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.leftExit(this);
        oddAdapter=new OddAdapter(this,R.layout.item_odd,orderBeanList);
        odd_rv.setAdapter(oddAdapter);
       /* odd_rv.setLayoutManager(new LinearLayoutManager(this));
        odd_rv.setAdapter(new OddAdapter(this));*/
        odd_rv.setLayoutManager(new XLinearLayoutManager(getActivity()));
        odd_rv.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light); // 设置下拉刷新的旋转圆圈的颜色
        odd_rv.enablePullRefresh(true); // 开启下拉刷新，默认即为true，可不用设置
        odd_rv.enableLoadDoneTip(true, R.string.load_done_tip); // 开启数据全部加载完成时的底部提示，默认为false
        odd_rv.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {

            @Override
            public void onPullRefresh() {
                odd_rv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isRefresh = true;
                        orderBeanList.clear();
                        presentPage = 1;
                        loadListData();
                    }
                }, 1500);
            }

            @Override
            public void onLoadMore() {
                odd_rv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isRefresh = false;
                        presentPage++;
                        loadListData();
                    }
                }, 1500);
            }
        });
        odd_rv.postRefreshing();

    }
    private void loadListData() {
        StockProto.get().getDistributeOrder("distribute",new HttpProtocol.Callback<DistributeOrderBean>() {
            @Override
            public void onSuccess(DistributeOrderBean orderBean) {
                onGetSuccess(orderBean);
            }
            @Override
            public void onFailed(int code, ErrorMsgException e) {
                Log.d("送货列表获取失败的数据---->",e.getMessage());

            }
        });

    }
    public void onGetSuccess(DistributeOrderBean orderBean) {
        orderBeanList.clear();
        pageSize=orderBean.data.totalpage;
        for(int i=0;i< orderBean.data.list.size();i++){
            DistributeOrderBean.DataBean.ListBean dataBean= orderBean.data.list.get(i);
            orderBeanList.add(dataBean);
        }
        oddAdapter.notifyDataSetChanged();
        oddAdapter.setOnItemClickListener(new OddAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, String id) {
                showLoadingDialog();
                getDetailData(id);

            }
        });

        if(isRefresh){
            if(odd_rv!=null){
                odd_rv.stopRefresh();
                odd_rv.enableLoadMore(pageSize > 1); // 当剩余还有大于0页的数据时，开启上拉加载更多
                if(orderBeanList.size() == 0){
                    Toast.makeText(getActivity(),"数据为空",Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }else {
            if(odd_rv!=null){
                odd_rv.stopLoadMore();
                odd_rv.enableLoadMore(pageSize > presentPage);
            }
        }

    }
    private void getDetailData(String id){
        Log.d("分单订单列表--->",""+id);
        StockProto.get().getDistributeDetailOrder(id, new HttpProtocol.Callback<DistributeOrderDetailBean>() {
            @Override
            public void onSuccess(DistributeOrderDetailBean distributeOrderDetailBean) {
               dismissLoadingDialog();
               showToast(distributeOrderDetailBean.msg);
               Intent intent=new Intent(DistributeOddActivity.this,DistributeOddDetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("detailData",distributeOrderDetailBean);
                intent.putExtras(bundle);
                startActivity(intent);



            }

            @Override
            public void onFailed(int code, ErrorMsgException e) {
                dismissLoadingDialog();
                showToast(e.getMessage());
            }
        });
    }
}
